/*
 * Copyright© 2003-2019 浙江汇信科技有限公司, All Rights Reserved.
 */
package com.icinfo.cloud.provider.punish.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.icinfo.cloud.provider.common.constant.PunishMainServicePortUriConstant;
import com.icinfo.cloud.provider.common.exception.BusinessException;
import com.icinfo.cloud.provider.common.utils.PunishMainClientUtil;
import com.icinfo.cloud.provider.punish.config.dto.RelationWorkflowInfoRequest;
import com.icinfo.cloud.provider.punish.config.model.ConfigBusiParamDocumentInfo;
import com.icinfo.cloud.provider.punish.config.model.ConfigBusiParamMain;
import com.icinfo.cloud.provider.punish.dto.ConfigBusiParamDocumentInfoRequest;
import com.icinfo.cloud.provider.punish.mapper.ConfigBusiParamDocumentInfoMapper;
import com.icinfo.cloud.provider.punish.service.IConfigBusiParamDocumentInfoService;
import com.icinfo.framework.common.web.Result;
import com.icinfo.framework.core.service.support.MyBatisServiceSupport;
import com.icinfo.framework.mybatis.mapper.entity.Example;
import com.icinfo.framework.tools.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * 描述: 业务关联文书配置 config_busi_param_document_info 对应的Service接口实现类.<br>
 *
 * @author framework generator
 * @date 2022年02月22日
 */
@Service
public class ConfigBusiParamDocumentInfoServiceImpl extends MyBatisServiceSupport implements IConfigBusiParamDocumentInfoService {
    /**
     * 日志记录器
     */
    private static final Logger logger = LoggerFactory.getLogger(ConfigBusiParamDocumentInfoServiceImpl.class);

    @Resource
    private ConfigBusiParamDocumentInfoMapper configBusiParamDocumentInfoMapper;


    /**
     * 根据文书目录编码获取详情
     *
     * @param documentCatalogCode
     * @return {@link ConfigBusiParamDocumentInfo}
     * @author liyafeng
     * @date 2022/2/25
     */
    @Override
    public ConfigBusiParamDocumentInfo getDetailByDocumentCatalogCode(String documentCatalogCode) {
        Example example = new Example(ConfigBusiParamDocumentInfo.class);
        example.createCriteria().andEqualTo("documentCatalogCode", documentCatalogCode).andEqualTo("isDeleted", "0");
        List<ConfigBusiParamDocumentInfo> configBusiParamDocumentInfos = configBusiParamDocumentInfoMapper.selectByExample(example);
        ConfigBusiParamDocumentInfo configBusiParamDocumentInfo = new ConfigBusiParamDocumentInfo();
        if (null != configBusiParamDocumentInfos && configBusiParamDocumentInfos.size() > 0) {
            configBusiParamDocumentInfo = configBusiParamDocumentInfos.get(0);
        } else {
            throw new BusinessException("未匹配到业务关联文书配置记录");
        }
        return configBusiParamDocumentInfo;
    }

    @Override
    public ConfigBusiParamMain getConfigBusiParamMain(RelationWorkflowInfoRequest request) {
        request.setApplyFieldCode("");
        Result result = PunishMainClientUtil.httpPost(PunishMainServicePortUriConstant.GET_CONFIG_BUSI_PARAM, request);

        JSONArray jsonArray =JSONArray.parseArray(result.getData().toString());
        List<ConfigBusiParamMain> configBusiParamMainList=jsonArray.toJavaList(ConfigBusiParamMain.class);
        if (ObjectUtil.isEmpty(configBusiParamMainList) && configBusiParamMainList.size()<0){
            throw  new BusinessException("未查询到匹配的参数配置");
        }
        ConfigBusiParamMain configBusiParamMain = configBusiParamMainList.get(0);
        return configBusiParamMain;

    }

    /**
     * 根据需送达文书目录编码获取送达回证配置详情
     *
     * @param sentDocumentCatalogCode
     * @return {@link ConfigBusiParamDocumentInfo}
     * @author zhengqiang
     * @date 2022/10/26
     */
    @Override
    public ConfigBusiParamDocumentInfo getSDHZDetailBySentDocumentCatalogCode(String sentDocumentCatalogCode) {
        Example example = new Example(ConfigBusiParamDocumentInfo.class);
        example.createCriteria().andEqualTo("matchSentDocumentCatalogCode", sentDocumentCatalogCode).andEqualTo("isDeleted", "0");
        List<ConfigBusiParamDocumentInfo> configBusiParamDocumentInfos = configBusiParamDocumentInfoMapper.selectByExample(example);
        if (null != configBusiParamDocumentInfos && configBusiParamDocumentInfos.size() > 0) {
            return configBusiParamDocumentInfos.get(0);
        } else {
            throw new BusinessException("未匹配到关联的送达回证文书配置记录");
        }
    }

    @Override
    public ConfigBusiParamDocumentInfo getDocumentInfoByDocumentKindCode(String busiParamId, String documentKindCode,String applyGroup) {
        Example example = new Example(ConfigBusiParamDocumentInfo.class);
        Example.Criteria criteria =example.createCriteria();
        criteria.andEqualTo("documentKindCode", documentKindCode);
        if(StringUtils.isNotBlank(busiParamId) ){
            criteria.andEqualTo("busiParamId", busiParamId);
        }
        if (StringUtils.isNotEmpty(applyGroup)){
            criteria.andEqualTo("applyGroup",applyGroup);
        }
        List<ConfigBusiParamDocumentInfo> configBusiParamMainList = configBusiParamDocumentInfoMapper.selectByExample(example);
        if (ObjectUtil.isNotEmpty(configBusiParamMainList)){
            return configBusiParamMainList.get(0);
        }
        return null;
    }

    @Override
    public List<ConfigBusiParamDocumentInfo> selectList(ConfigBusiParamDocumentInfoRequest request) {
        if (Objects.isNull(request)) {
            return Collections.emptyList();
        }
        Example example = new Example(ConfigBusiParamDocumentInfo.class);
        Example.Criteria criteria = example.createCriteria();
        if (StrUtil.isNotEmpty(request.getApplyGroup())) {
            criteria.andLike("applyGroup", "%" + request.getApplyGroup() + "%");
        }
        if (StrUtil.isNotEmpty(request.getBusiParamId())) {
            criteria.andEqualTo("busiParamId", request.getBusiParamId());
        }
        if (Boolean.TRUE.equals(request.getTemplateAppCodeNotNull())) {
            criteria.andIsNotNull("templateAppCode").andNotEqualTo("templateAppCode", "");
        }
        return configBusiParamDocumentInfoMapper.selectByExample(example);
    }

}
